<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty truncate modifier plugin
 *
 * Type:     modifier<br>
 * Name:     truncate<br>
 * Purpose:  Truncate a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and
 *           appending the $etc string or inserting $etc into the middle.
 * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
 *          truncate (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */
 /*
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
                                  $break_words = false, $middle = false)
{
    if ($length == 0)
        return '';

    if (strlen($string) > $length) {
        $length -= min($length, strlen($etc));
        if (!$break_words && !$middle) {
            $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
        }
        if(!$middle) {
            return substr($string, 0, $length) . $etc;
        } else {
            return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
        }
    } else {
        return $string;
    }
}
*/

function smarty_modifier_truncate($string,$cutlen = 80,$endtag = '... ',$charset = 'utf-8')
{
	/*
	+-----------------------------------
	+	Total length
	+-----------------------------------
	*/
	$length = strlen($string);
	/*
	+-----------------------------------
	+	Full return
	+-----------------------------------
	*/
	if($length <= $cutlen) 
	{
		return $string;
	}
	else
	{
		$startid = 0;
		/*
		+-------------------------------
		+	UTF8 CODE, longer than gbk
		+-------------------------------
		*/
		if($charset == 'utf-8') 
		{
			$endid = $ordid = $cuts = 0;
			while ($endid < $length) 
			{
				$ordno = ord($string[$endid]);
				if($ordno == 9 || $ordno == 10 || (32 <= $ordno && $ordno <= 126)) 
				{
					$ordid = 1; $endid++; $cuts++;
				}
				elseif(194 <= $ordno && $ordno <= 223)
				{
					$ordid = 2; $endid += 2; $cuts += 2;
				}
				elseif(224 <= $ordno && $ordno < 239)
				{
					$ordid = 3; $endid += 3; $cuts += 2;
				}
				elseif(240 <= $ordno && $ordno <= 247)
				{
					$ordid = 4; $endid += 4; $cuts += 2;
				}
				elseif(248 <= $ordno && $ordno <= 251)
				{
					$ordid = 5; $endid += 5; $cuts += 2;
				}
				elseif($ordno == 252 || $ordno == 253)
				{
					$ordid = 6; $endid += 6; $cuts += 2;
				}
				else
				{
					$endid++;
				}
				if ($cuts >= $cutlen) 
				{
					break;
				}
			}
			if ($cuts >= $cutlen)
			{
				$endid -= $ordid;
			}
		}
		/*
		+-------------------------------
		+	GBK,BIG5 and so on
		+-------------------------------
		*/
		else
		{
			$endid = 0;
			for ($i=0;$i<$cutlen-2;$i++)
			{
				if (ord($string[$i]) > 127)
				{
					$endid += 2;
					$i++;
				}
				else
				{
					$endid++;
				}
			}
		}
		/*
		+-------------------------------
		+	Return String;
		+-------------------------------
		*/
		return substr($string,$startid,$endid).($endtag ? $endtag : '');
	}
}
/* vim: set expandtab: */

?>
